﻿<?php
/********************************************************
 *
 * 此文件的作用:
 *		后台控制器
 *
 * @copyright Copyright (c) 2008-2009
 * @author 刘昌功 lcg635@126.com
 *
 ********************************************************/
defined('APP_DIR') or die('404 Not Found');

FLEA::loadClass('Common_Action');

class Controller_Worker extends Common_Action {
	function __construct($ctlName)   {
		parent::__construct($ctlName);
		$this->tplDir = TPL_F;
		$this->checkLogined();
	}

	function index() {}

	function hasRegister(){
		$_modWorker = FLEA::getSingleton('Model_Worker');
		$_modUser = FLEA::getSingleton('Model_User');
		if ( $_modWorker->hasRegister($this->user['userid']) > 0 )//已申请过
			js_alert('您已经申请过了',null,$this->_url('viewWorker'));
		else 
			redirect($this->_url('registerWorker'));
	}

/*
	function register() {
		$_modWorker = FLEA::getSingleton('Model_Worker');
		$_modUser = FLEA::getSingleton('Model_User');
		$userData = $_modUser->getUserInfo($this->user['userid'], 'work');
		$workerData = $_modWorker->getWorkerInfo($this->user['userid']);
                     
		if ( $this->_isPOST() ) {			
			$seri = $this->getSeri($data['college'], $data['num']);
			$data['seri'] = $seri;
			$result = $_modWorker->register($_POST, 'register', $data);
			if ( is_array($result) ) {
				$err = $result['err'][0];
				js_alert($err, null, $this->_url('register'));
			} else
				$this->_out('worker/process');
		} else {
			if ( null == $userData['pic'] )
				js_alert('您的基本资料还不完整，请先进行修改(您没有上传照片或照片大于300K，没有上传成功)', null, $url('user', 'modify'));
			if ( !empty($workerData) ) redirect($this->_url('modify'));
			if ( $this->saveGet('agree') != 1 )
				$this->_out('worker/protocol');
			else {
				$viewData = array(
					'form' => $_modWorker->getForm($userData, $workerData),
					'user' => $userData,
					'action' => url('worker', 'register')
				);
				$this->_out('worker/register', $viewData);
			}
		}
	}
*/
	function register() {
		$this->_out('worker/register_notice', $viewData);
	}
/*	
	function printTable() {
		if ( $this->user['userid'] == null ) {
			js_alert('您还没有登录，请先登录系统', null, url());
		}

		$_modUser = FLEA::getSingleton('Model_User');
		$viewData = array('user' => $_modUser->getUserInfo($this->user['userid'], 'work') );

		$this->_out('print', $viewData);
	}
*/	
	function getSeri($college = null,$grade = null){
		$grade=$grade[0].$grade[1].$grade[2].$grade[3];
		//dump($grade);
		$_modtest= FLEA::getSingleton('Model_Worker');
		$total = $_modtest->getSeri($college,$grade)+1;
		//dump($total);
		if($total>0 && $total<10) $seri='00'.$total;
		elseif ($total>9 && $total<100) $seri='0'.$total;
		//dump($seri);
		return $seri;
		
	}

	function modify() {
		$_modWorker = FLEA::getSingleton('Model_Worker');
		$_modUser = FLEA::getSingleton('Model_User');
		$userData = $_modUser->getUserInfo($this->user['userid'], 'work');
		$workerData = $_modWorker->getWorkerInfo($this->user['userid']);
		
		if (empty($workerData)) redirect($this->_url('register'));
		
		if ( $this->_isPOST() ) {
			$seri = $this->getSeri($data['college'], $data['num']);
			$data['seri'] = $seri;
			$result = $_modWorker->register($_POST, 'modify', $data);
			js_alert('修改成功', null, url('worker', 'prePrint'));
		} else {
			$viewData = array(
				'form' => $_modWorker->getForm($userData, $workerData),
				'user' => $userData,
				'action' => url('worker', 'modify')
			);
			$this->_out('worker/register', $viewData);
		}
	}
	
	function prePrint(){//查看特定员工
		$_modWorker = FLEA::getSingleton('Model_Worker');
		$_modUser = FLEA::getSingleton('Model_User');
		$_modProcess = FLEA::getSingleton('Model_Process');
		$viewData = array(
			'user' => $_modUser->getUserInfo($this->user['userid'], 'work'), 
			'process' => $_modProcess->getList($this->saveGet('user_id')), 
			'processForm' => $_modProcess->getForm($this->saveGet('user_id')) 
			);
		//dump($viewData);
		$this->_out('worker/viewWorker', $viewData);
	}
	
	function note() {
		$this->_out('worker/note');
	}

}